package com.ziki.bswicket;

import java.util.Set;

import org.apache.wicket.Page;
import org.apache.wicket.markup.head.IHeaderResponse;
import org.apache.wicket.markup.head.filter.JavaScriptFilteredIntoFooterHeaderResponse;
import org.apache.wicket.markup.html.IHeaderResponseDecorator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.ziki.bswicket.constant.WebConst;
import com.ziki.common.util.Properties.PropertiesUtil;
import com.ziki.common.util.string.StrUtil;
import com.ziki.framework.wicket.FwWebApplication;

public class BsWebApp extends FwWebApplication {
    private static Logger log = LoggerFactory.getLogger(BsWebApp.class);
    private PropertiesUtil pages;

    public BsWebApp() {
    }

    @SuppressWarnings("unchecked")
    @Override
    public Class<? extends Page> getHomePage() {
        try {
            return (Class<Page>) Class.forName(pages.getProperty(WebConst.HOME_PAGE_KEY));
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
    }

    @SuppressWarnings("unchecked")
    @Override
    protected void init() {
        getDebugSettings().setDevelopmentUtilitiesEnabled(true);
        setHeaderResponseDecorator(new IHeaderResponseDecorator() {
            @Override
            public IHeaderResponse decorate(IHeaderResponse response) {
                // load all JavaScript resources in the page footer (after </body>)
                return new JavaScriptFilteredIntoFooterHeaderResponse(response, "footerJS");
            }
        });

        pages = new PropertiesUtil(WebConst.PAGE_URL);
        Set<Object> keys = pages.getKeys();
        for (Object key : keys) {
            try {
                this.mountPage(StrUtil.parseStr(key), (Class<Page>) Class.forName(pages.getProperty(key)));
            } catch (Exception e) {
                log.error("", e);
            }
        }
    }
}
